package com.chen.controller.admin;

import com.chen.pojo.Blog;
import com.chen.pojo.User;
import com.chen.service.BlogService;
import com.chen.service.TagService;
import com.chen.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    BlogService blogService;

    @Autowired
    TypeService typeService;

    @Autowired
    TagService tagService;

    @GetMapping("/blogs")
    public String blogs(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        String orderBy = "update_time desc";
        PageHelper.startPage(pageNum, 10, orderBy);
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogService.listBlog());
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page", blogPageInfo);
        return "admin/blogs";
    }

    @PostMapping("/blogs/search")
    public String search(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        String orderBy = "update_time desc";
        PageHelper.startPage(pageNum, 10, orderBy);
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogService.listBlog());
        model.addAttribute("page", blogPageInfo);
        return "admin/blogs :: blogList";
    }

    // 跳转到新增博客
    @GetMapping("/blogs/input")
    public String input(Model model) {
        model.addAttribute("blog", new Blog());
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        return "admin/blogs-input";
    }

    // 新增博客功能
    @PostMapping("/blogs")
    public String post(Blog blog,
                       RedirectAttributes attributes,
                       HttpSession session) {
        blog.setUser((User) session.getAttribute("user"));
        // 设置用户Id
        blog.setUserId(blog.getUser().getId());
        // 设置blog中的typeId
        blog.setTypeId(blog.getType().getId());
        blog.setTypeName(blog.getType().getName());
        int i = blogService.saveBlog(blog);
        if (i == 0) {
            attributes.addFlashAttribute("message", "操作失败");
        } else {
            attributes.addFlashAttribute("message", "操作成功");
        }

        return "redirect:/admin/blogs";
    }


    // 跳转到新增博客
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {
        model.addAttribute("blog", new Blog());
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());

        return "admin/blogs-input";
    }

    //    编辑修改文章
    @PostMapping("/blogs/{id}")
    public String editPost(Blog blog, RedirectAttributes attributes) {
        int b = blogService.updateBlog(blog.getId(), blog);
        if(b ==0){
            attributes.addFlashAttribute("message", "修改失败");
        }else {
            attributes.addFlashAttribute("message", "修改成功");
        }
        return "redirect:/admin/blogs";
    }

    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/blogs";
    }


}
